/*
 * Copyright (c) 2010 Mathias Garbe <code@mathias-garbe.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "client.hpp"
#include "GameSelectWindow.hpp"

GameSelectWindow::GameSelectWindow(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
    QWidget::setWindowModality(Qt::ApplicationModal);

    ui.setupUi(this);

    connect(this->ui.pushStart, SIGNAL(clicked()), this, SLOT(startGame()));
    connect(this->ui.treePaths, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(startGame(QModelIndex)));
    connect(this->ui.pushCancel, SIGNAL(clicked()), this, SLOT(onCancel()));

    this->game = -1;
    this->ui.treePaths->setRootIsDecorated(false);
    this->ui.treePaths->setAlternatingRowColors(true);
    this->treeModel = new QStandardItemModel(0, 3, parent);
    this->treeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
    this->treeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Exe path"));
    this->treeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Arguments"));
    this->ui.treePaths->setModel(this->treeModel);

    this->ui.treePaths->setColumnWidth(0, 105);
    this->ui.treePaths->setColumnWidth(1, 300);
    this->ui.treePaths->setColumnWidth(2, 80);
}

GameSelectWindow::~GameSelectWindow()
{

}

AdvancedGameInfo GameSelectWindow::show(int game)
{
    // Move window to middle of the screen
    QRect screen = QApplication::desktop()->screenGeometry();
    this->move(screen.width()/2 - this->width()/2, screen.height()/2 - this->height()/2);
    QWidget::show();

    std::stringstream tmpTitle;
    tmpTitle << "Select a version of " << Games::getGameShortNameString(game) << " to start";
    this->setWindowTitle(tmpTitle.str().c_str());

    this->game = game;
    this->selectedGame = -1;

    // clear treeview
    int max = this->treeModel->rowCount();
    for(int i=0; i<max; i++)
    {
        this->treeModel->removeRows(0, 1);
    }

    std::vector<AdvancedGameInfo> games;
    games = AdvancedGameInfo::loadGameInfos(game);

    if(games.size() == 1)
    {
        this->hide();
        return AdvancedGameInfo(games[0].getType(), games[0].getExe(), games[0].getArgs());
    }

    for (std::vector<AdvancedGameInfo>::iterator it = games.begin(); it!=games.end(); ++it) 
    {
        AdvancedGameInfo tmpGameInfo = (*it);

        int s = this->treeModel->rowCount();
        this->treeModel->insertRow(s);
        this->treeModel->setData(treeModel->index(s, 0), tmpGameInfo.getDescription().c_str());
        this->treeModel->setData(treeModel->index(s, 1), tmpGameInfo.getExe().c_str());
        this->treeModel->setData(treeModel->index(s, 2), tmpGameInfo.getArgs().c_str());
    }

    this->closed = false;

    while(!this->closed)
    {
        Qt::QAppInstance()->processEvents(QEventLoop::AllEvents, 1);
        Qt::QAppInstance()->flush();
        Sleep(10);
    }

    this->hide();

    if(this->selectedGame == -1)
    {
        return AdvancedGameInfo(Games::NOGAME, "", "");
    }
    else
    {
        return AdvancedGameInfo(games[this->selectedGame].getType(), games[this->selectedGame].getExe(), games[this->selectedGame].getArgs());
    }
}


void GameSelectWindow::startGame(QModelIndex tmp)
{
    this->startGame();
}

void GameSelectWindow::startGame()
{
    QModelIndexList list = this->ui.treePaths->selectionModel()->selectedIndexes();

    int row = -1;
    foreach(QModelIndex index, list)
    {
        row = index.row();
        break;
    }

    if(row == -1)
    {
        QMessageBox msgBox;
        msgBox.setText("Please select wich game to start.");
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.setIcon(QMessageBox::Critical);
        int ret = msgBox.exec();
        return;
    }

    this->selectedGame = row;
    this->closed = true; 
}

void GameSelectWindow::onCancel()
{
    this->closed = true; 
}

void GameSelectWindow::closeEvent(QCloseEvent *event)
{
    this->closed = true;
}
